package com.yonyou.cyxdms.basedata.service.service.product;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyxdms.basedata.service.dto.vehicleprice.ProductPriceDTO;
import com.yonyou.cyxdms.basedata.service.entity.product.VsProductPricePO;
import com.yonyou.cyxdms.basedata.service.repository.product.VsProductMapper;
import com.yonyou.cyxdms.basedata.service.repository.product.VsProductPriceMapper;
import com.yonyou.cyxdms.basedata.service.util.MapUtils;
import com.yonyou.cyxdms.basedata.service.vo.productprice.ProductPriceVO;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 整车物料号价格维护 服务实现类
 * </p>
 *
 * @author renwd
 * @since 2019-07-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class VsProductPriceServiceImpl extends ServiceImpl<VsProductPriceMapper, VsProductPricePO> implements VsProductPriceService {

    @Autowired
    VsProductPriceMapper vsProductPriceMapper;

    @Autowired
    VsProductMapper vsProductMapper;

    /**
     * 主数据价格维护
     *
     * @param page
     * @param queryParam
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.productprice.ProductPriceVO>
     * @author renwd
     * @since 2019/7/23
     */
    @Override
    public IPage<ProductPriceVO> listVehicleMasterPrice(Page page, Map<String, Object> queryParam) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        MapUtils.addParameter(queryParam, loginInfoDto.getOwnerCode(), loginInfoDto.getAppId(), loginInfoDto.getGroupCode());
        page.setRecords(vsProductPriceMapper.listVehicleMasterPrice(page, queryParam));
        return page;
    }

    /**
     * 主数据价格新增
     *
     * @param productPriceDTO
     * @return java.lang.Integer
     * @author renwd
     * @since 2019/7/23
     */
    @Override
    public Integer addProductPrice(ProductPriceDTO productPriceDTO) {
        List<ProductPriceDTO> priceDto = productPriceDTO.getProductPriceList();
        priceDto.stream().forEach(priceDTO -> {
            VsProductPricePO vsProductPricePo = new VsProductPricePO();
            vsProductPricePo.setOwnerCode(priceDTO.getDealerCode());
            vsProductPricePo.setDirectivePrice(priceDTO.getDirectivePrice());
            vsProductPricePo.setDataSources(priceDTO.getDataSources());
            vsProductPriceMapper.insert(vsProductPricePo);
        });
        return 1;
    }

    /**
     * 主数据价格修改
     *
     * @param id
     * @param productPriceDTO
     * @return java.lang.Integer
     * @author renwd
     * @since 2019/7/23
     */
    @Override
    public Integer modifyProductPrice(Long id, ProductPriceDTO productPriceDTO) {
        VsProductPricePO vsProductPricePo = vsProductPriceMapper.selectById(id);
        vsProductPricePo.setDirectivePrice(productPriceDTO.getDirectivePrice());
        return vsProductPriceMapper.updateById(vsProductPricePo);
    }

    /**
     * 主数据价格批量更新
     *
     * @param productPriceDTO
     * @return java.lang.Integer
     * @author renwd
     * @since 2019/7/23
     */
    @Override
    public Integer updateProductPrice(ProductPriceDTO productPriceDTO) {
        String[] ids = productPriceDTO.getProductPriceIds().split(",");
        for (int i = 0, j = ids.length; i < j; i++) {
            VsProductPricePO vsProductPricePo = vsProductPriceMapper.selectById(ids[i]);
            vsProductPricePo.setDirectivePrice(productPriceDTO.getDirectivePrice());
            vsProductPriceMapper.updateById(vsProductPricePo);
        }
        return 1;
    }

}
